package com.mushuilingfeng.ddd.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.mushuilingfeng.ddd.user.bo.UserQueryBO;
import com.mushuilingfeng.ddd.user.cover.IDataCover;
import com.mushuilingfeng.ddd.user.domain.base.BusinessException;
import com.mushuilingfeng.ddd.user.domain.base.DataPageResult;
import com.mushuilingfeng.ddd.user.domain.base.Ii8nCode;
import com.mushuilingfeng.ddd.user.domain.user.entity.User;
import com.mushuilingfeng.ddd.user.domain.user.repository.IUserRepository;
import com.mushuilingfeng.ddd.user.service.IUserManagerService;
import com.mushuilingfeng.ddd.user.vo.UserVO;
import com.mushuilingfeng.spring.common.PageResult;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author 沐水淋风
 * @date 2024/10/20
 */
@Service
@AllArgsConstructor
public class UserManagerServiceImpl implements IUserManagerService {
    private final IUserRepository userRepository;
    
    @Override
    public void lockOrUnlockUser(Long userId, boolean lock) {
        User user = userRepository.get(userId);
        if (null == user) {
            throw new BusinessException(Ii8nCode.USER_NOT_EXIST);
        }
        if (lock) {
            user.lock();
        } else {
            user.unlock();
        }
    }
    
    @Override
    public PageResult<UserVO> users(UserQueryBO bo) {
        DataPageResult<User> users = userRepository.list(IDataCover.INSTANCE.toUserQueryDto(bo));
        PageResult<UserVO> pageResult = new PageResult<>();
        pageResult.setPageNum(users.getPageNum());
        pageResult.setPageSize(users.getPageSize());
        pageResult.setTotal(users.getTotal());
        if (!CollectionUtil.isEmpty(users.getData())) {
            pageResult.setData(IDataCover.INSTANCE.toUserVOList(users.getData()));
        }
        return pageResult;
    }
    
}
